package it.unibo.deis.lia.mmhc.spm;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.BindException;

import java.net.Socket;
import java.util.Vector;

public class ServiceServer extends Thread {

	private Vector<String> goHop;
	private Vector<String> backHop;
	int port;
	private String fornitore;
	private String richiedente;
	private boolean go;
	private String name;
	private Socket input;
	private boolean changed = false;
	private SocketVector socketVector=null;

	public ServiceServer(Vector<String> goHop, Vector<String> backHop,
			int port, String fornitore, String richiedente, boolean go,
			String name) {
		super();
		this.goHop = goHop;
		this.backHop = backHop;
		this.port = port;
		this.fornitore = fornitore;
		this.richiedente = richiedente;
		this.go = true;
		this.name = name;
		input = null;
		socketVector=SocketVector.getInstance();
		
	}

	public void setSoket(Socket in) {
		input = in;
		changed = true;
		// this.start();
		// socketchaged();
		System.err.println("\t socket settaataaaaa!!!!!" + changed);
	}

	public void run() {
		try {
			// ServerSocket ss = null;
			// try {
			// ss = new ServerSocket(port);
			//
			// } catch (BindException e) {
			// go = false;
			// }

			System.err.println("Service server avviato!");
			while (go) {
				sleep(500);
				if (changed) {
					System.err.println("service server preaccept");
					// Socket inp = ss.accept();
					System.err.println("service server postaccept");
					ObjectInputStream ois = new ObjectInputStream(input
							.getInputStream());
					// System.err.println("\tRicevuto oggetto da " +
					// remoteSenderIP
					// + " alle " + System.currentTimeMillis());
					Object obj = ois.readObject();
					ServicePacket packet = new ServicePacket(name, port,
							fornitore, richiedente, goHop, backHop, obj);
					// input.close();
					
					packet.setGo(false);
					String hop = packet.nextHop();
					
							Socket out = socketVector.getSoket(hop,2500);
						
						
					ObjectOutputStream oos = new ObjectOutputStream(out
							.getOutputStream());
					oos.flush();
					oos.writeObject(packet);
					oos.flush();
					// out.close();
					changed = false;
				}
			}
		} catch (Exception e) {
			System.err.println("\tService Server " + e);
		}

	}

	public int getPort() {
		return port;
	}

	public String getFornitore() {
		return fornitore;
	}

	public String getRichiedente() {
		return richiedente;
	}

	public String getName1() {
		return name;
	}

	// public void socketchaged(){
	// try{
	// System.err.println("service server preaccept");
	//		
	// //Socket inp = ss.accept();
	// System.err.println("service server postaccept");
	// ObjectInputStream ois=new ObjectInputStream(input.getInputStream());
	// // System.err.println("\tRicevuto oggetto da " + remoteSenderIP
	// // + " alle " + System.currentTimeMillis());
	// Object obj = ois.readObject();
	// ServicePacket packet = new ServicePacket(name, port, fornitore,
	// richiedente, goHop, backHop, obj);
	// //input.close();
	// int attempts = 5;
	// packet.setGo(false);
	// String hop = packet.nextHop();
	// Socket out = null;
	// do {
	// try {
	// out = new Socket(hop, 2500);
	// } catch (BindException e) {
	// } catch (Exception e) {
	// Thread.sleep(1500);
	// attempts--;
	// if (attempts == 0)
	// throw e;
	// System.err
	// .println("Impossibile aprire o trovare socket di risposta "
	// + e);
	// }
	// } while (attempts > 0 && out == null);
	// ObjectOutputStream oos = new ObjectOutputStream(out
	// .getOutputStream());
	// oos.flush();
	// oos.writeObject(packet);
	// oos.flush();
	// }catch (Exception e) {
	// System.err.println("Errore socketchanged!!!");
	// }
	// }

}
